import WeCropper from '../../we-cropper/we-cropper.js'

const device = wx.getSystemInfoSync()
Page({
  data: {
    cropperOpt: {
      id: 'cropper',
      width: device.windowWidth,
      height: device.windowWidth,
      scale: 2.5,
      zoom: 8
    }
  },
  touchStart (e) {
    this.wecropper.touchStart(e)
  },
  touchMove (e) {
    this.wecropper.touchMove(e)
  },
  touchEnd (e) {
    this.wecropper.touchEnd(e)
  },
  //确定保存图片
  getCropperImage () {
    this.wecropper.getCropperImage((avatar) => {
      if (avatar) {
        //  获取到裁剪后的图片 
        console.log(avatar)
        //这里要把图片上传
        // wx.redirectTo({
        //   url: `../index/index?avatar=${avatar}`
        // })
      } else {
        console.log('获取图片失败，请稍后重试')
      }
    })
  },
  //uploadTap  重新选择
  uploadTap () {
    const self = this

    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success (res) {
        const src = res.tempFilePaths[0]
        //  获取裁剪图片资源后，给data添加src属性及其值 cgetCropperImage

        self.wecropper.pushOrign(src)
      }
    })
  },
  onLoad (option) {
    const { cropperOpt } = this.data

    if (option.src) {
      console.log(option.src)
      cropperOpt.src = option.src
      new WeCropper(cropperOpt)
        .on('ready', (ctx) => {
          
        })
        .on('beforeImageLoad', (ctx) => {
         
          wx.showToast({
            title: '上传中',
            icon: 'loading',
            duration: 20000
          })
        })
        .on('imageLoad', (ctx) => {
        
          wx.hideToast()
        })
        .on('beforeDraw', (ctx, instance) => {
      
        })
         .updateCanvas()
    }
  }
})
